package com.gopay.scheduler.service.cbrmb.impl;

import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.cbrmb.CbRmbWildCardAssistDtlDAO;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.domain.cbrmb.internationalcard.CbRmbWildCardAssistDtl;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.scheduler.service.cbrmb.CbRmbIntlCardSettleAmtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 *
 * @ClassName: CbRmbIntlCardSettleAmtService
 * @Description: 外卡订单清算金额更新
 * @author b.zou
 * @date 2017-11-8
 */
@Service("cbRmbIntlCardSettleAmtService")
public class CbRmbIntlCardSettleAmtServiceImpl implements CbRmbIntlCardSettleAmtService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "cpsGeneralMainOrderQueryDAO")
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderQueryDAO;

    @Resource(name = "cbRmbWildCardAssistDtlDAO")
    private CbRmbWildCardAssistDtlDAO cbRmbWildCardAssistDtlDAO;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    public void updateSettleAmt(String gopayOrderId) {
        try {
            CpsGenMainOrder main = cpsGeneralMainOrderQueryDAO.findByGopayOrderId(gopayOrderId);
            List<CpsGenMainOrder> mainOrders = cpsGeneralMainOrderQueryDAO.findListByRelevanceGopayOrderIdAndGopayIntTxnCd(gopayOrderId, IntTxnCd._01742.value);
            BigDecimal settleAmt = main.getMerTxnAmt();
            if (!mainOrders.isEmpty()) {
                for (CpsGenMainOrder refund : mainOrders) {
                    if ((!TxnStaCode.TXN_STA_CD_20000.equals(refund.getTxnStaCd())) || (!TxnStaCode.TXN_STA_CD_20003.equals(refund.getTxnStaCd()))) {
                        settleAmt = settleAmt.subtract(refund.getMerTxnAmt());
                    }
                }
            }
            CbRmbWildCardAssistDtl assistDtl = cbRmbWildCardAssistDtlDAO.findByGopayOrderId(gopayOrderId);
            assistDtl.setUpperSettleAmt(settleAmt);
            assistDtl.setSettleFlag("1");
            assistDtl.setUpdateTime(new Date());
            cbRmbWildCardAssistDtlDAO.update(assistDtl);
            if (!mainOrders.isEmpty()) {
                for (CpsGenMainOrder refund : mainOrders) {
                    CbRmbWildCardAssistDtl assistDtl2 = cbRmbWildCardAssistDtlDAO.findByGopayOrderId(refund.getGopayOrderId());
                    assistDtl2.setUpperSettleAmt(refund.getMerTxnAmt());
                    assistDtl2.setUpdateTime(new Date());
                    cbRmbWildCardAssistDtlDAO.update(assistDtl2);
                }
            }
            logger.error("订单清算金额更新完成，订单号：{}", gopayOrderId);
        } catch (Exception e){
            logger.error("更新订单异常，订单号：{}，异常信息：{}", new Object[]{gopayOrderId,e});
        }
    }
}
